package com.lc.user.express.my;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.adapter.CouponAdapter;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetMyCoupon;
import com.lc.user.express.model.CouponModel;
import com.lc.user.express.view.pull.PullToRefreshBase;
import com.lc.user.express.view.pull.PullToRefreshListView;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/14.
 */
public class MyCouponActivity extends BaseSecondActivity {
    private PullToRefreshListView prl;
    private TextView tv_youhuijuantishi;
    private CouponAdapter couponAdapter;
    private List<CouponModel> list = new ArrayList<>();
    private int ispage;//是否有下一页
    private int nextpage;//分页

    private GetMyCoupon getMyCoupon = new GetMyCoupon(AppContext.myPreferences.getUid(), 1, new AsyCallBack<GetMyCoupon.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetMyCoupon.Info info) throws Exception {
            ispage = info.ispage;
            nextpage = info.nextpage;
            if (type == 0) {
                list.clear();
            }
            if (info.list.size() == 0) {
                tv_youhuijuantishi.setText("无可用优惠券");
            }
            list.addAll(info.list);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {
            super.onEnd(toast, type);
            couponAdapter.updateListView(list);
            prl.onPullUpRefreshComplete();
            prl.onPullDownRefreshComplete();
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon);
        tv_youhuijuantishi = (TextView) findViewById(R.id.tv_youhuijuan_tishi);
        prl = (PullToRefreshListView) findViewById(R.id.coupon_list);
        getData(1, true, 0);
        setTitle("我的优惠券");
        prl.setScrollLoadEnabled(true);
        prl.setPullLoadEnabled(false);
        prl.setPullRefreshEnabled(true);

        prl.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                getData(1, false, 0);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (ispage == 1) {
                    getData(nextpage, false, 1);
                } else {
                    try {
                        prl.onPullDownRefreshComplete();
                        prl.onPullUpRefreshComplete();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        ListView lv = prl.getRefreshableView();

        lv.setDivider(null);
        lv.setDividerHeight(0);
        lv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        couponAdapter = new CouponAdapter(cnt, list);
        lv.setAdapter(couponAdapter);

    }

    private void getData(int page, boolean isShow, int type) {
        getMyCoupon.page = page;
        getMyCoupon.execute(cnt, isShow, type);
    }
}
